package com.shenganyuan.smac.common.module.imager;

import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;

import com.shenganyuan.smac.R;
import com.shenganyuan.smac.common.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;


/**
 * @author Damon.Han
 * @Description: 图片预览activity
 */
public class ActImgViewer extends BaseActivity {
    public static final int requestCode = 9999;
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    private HackyViewPager mPager;
    private String dateStr = "";
    private int pagerPosition;
    private ArrayList<String> urls;
    private ImagePagerAdapter mImagePagerAdapter;
    @BindView(R.id.common_toolbar_center_title)
    TextView commonToolbarCenterTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getLayoutId() {
        return R.layout.act_img_viewer;
    }

    @Override
    public void initUI(Bundle savedInstanceState) {

    }

    @Override
    public void initData() {
        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        urls = (ArrayList<String>) getIntent().getSerializableExtra(EXTRA_IMAGE_URLS);
        if (urls == null || urls.size() == 0) {
            return;
        }
        mPager = findViewById(R.id.pager);
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int state) {
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                commonToolbarCenterTitle.setText((position + 1) + "/" + urls.size());
            }

        });
        if (urls == null) {
            urls = new ArrayList<String>();
        }
        mImagePagerAdapter = new ImagePagerAdapter(mContext, urls);
        mPager.setAdapter(mImagePagerAdapter);
        mPager.setCurrentItem(pagerPosition);
        int initIndex = (pagerPosition + 1);
        if (initIndex > urls.size()) {
            initIndex = urls.size();
        }
        setTitle(initIndex + "/" + urls.size());
    }
}
